package com.storm.future.red.common;

import java.util.LinkedList;
import java.util.List;

/**
 * @author gu
 * @date 2020/3/16 10:22
 */
public abstract class AbstractBattleBase implements BattleBase {

    /** 可以建造的建筑 */
    protected List<Building> allowableBuilding = new LinkedList<>() ;
    /** 可以建造的防御设施 */
    protected List<Defender> allowableDefender = new LinkedList<>() ;
    /** 可以建造的士兵 */
    protected List<Soldier> allowableSoldier = new LinkedList<>() ;
    /** 可以建造的战斗装甲 */
    protected List<Tank> allowableTank = new LinkedList<>() ;
    /**
     * 电量是否充足的标志
     * */
    private boolean powerEfficient;
    /**
     * 当前电量负载
     * 当电量超标的时候，powerEfficient标志应改为false。
     * */
    private int powerLoad = 0;
    List<Building> buildings = new LinkedList<>();
    /**
     * 拥有的电力
     * */
    protected int power ;
    /**
     * 拥有的金钱
     * */
    protected int money ;

    public AbstractBattleBase() {
        power = 0 ;
        money = 1000;
    }

    public AbstractBattleBase(int money){
        power = 0 ;
        this.money = money ;
    }

    @Override
    public void construct(Building building) {
        if (!building.construct()) {
            return;
        }
        buildings.add(building);
    }

    @Override
    public void increasePower(int power) {
        assert power > 0 ;
        this.power += power ;
        if(powerEfficient == false){
            powerEfficient = this.power >= powerLoad ;
            if(powerEfficient == true){
                for (Building building : buildings) {
                    building.active();
                }
            }
        }
    }

    @Override
    public void occupyPower(int power) {
        assert power < 0;
        this.power -= power ;
        if(powerEfficient == true){
            powerEfficient = this.power >= powerLoad ;
            if(powerEfficient == false){
                for (Building building : buildings) {
                    building.shutdown();
                }
            }
        }
    }
}